package com.ruoyi.algorithm.util;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * @author dyk
 * @date 2021/7/12 11:35
 * @description
 */
@Slf4j
public class VersionUtil {

    public static String upgradeVersion(String version)
    {
        String upgradeVersion;
        if (StringUtils.isEmpty(version))
        {
            version = "1.0.0.0";
        }
        //将版本号拆解成整数数组
        List<String> strArr = Splitter.on(".").splitToList(version);
        Integer[] ints = new Integer[strArr.size()];
        for(int i=0 ;i<strArr.size(); i++) {
            ints[i] = Integer.parseInt(strArr.get(i));
        }
        //递归调用
        upgradeVersion(ints, ints.length - 1);

        //数组转字符串
        upgradeVersion = Joiner.on(".").join(ints);

        return upgradeVersion;
    }
    private static void upgradeVersion(Integer[] ints, int index)
    {
        if (index == 0) {
            ints[0] = ints[0] + 1;
        } else {
            int value = ints[index] + 1;
            if (value < 10) {
                ints[index] = value;
            } else {
                ints[index] = 0;
                upgradeVersion(ints, index - 1);
            }
        }
    }

    public static void main(String[] args) {
        log.info("version:1.0.0.0|"+upgradeVersion("1.0.0.0"));
        log.info("version:1.0.0.9|"+upgradeVersion("1.0.0.9"));
        log.info("version:1.0.1.9|"+upgradeVersion("1.0.1.9"));
        log.info("version:1.0.9.9|"+upgradeVersion("1.0.9.9"));
        log.info("version:1.9.9.9|"+upgradeVersion("1.9.9.9"));
    }
}
